<template>
	<view class='page_cont'>
		<view class='top_wrap' id="top">
			<view class='search_wrap'>
				<view class='search_box'>
					<image class='icon' src="/static/icon26.png"></image>
					<input type="text" class='input' :focus='focus' :placeholder="show_page==1?'搜索职位/单位名称':'搜索职位名称'" confirm-type="search" @input="input_search" @confirm="confirm_search">
				</view>
				<view class='search_btn' @click="confirm_search">搜索</view>
			</view>
			<view class='check_warp' v-if="show_page==1">
				<view class='item' :id="'item'+index" :class="cont_index==index?'active':''" v-for="(item,index) in checks" :key="index" @click="check_cont(index)">
					<view class='text'>{{item}}</view>
					<view class='line' v-if="cont_index==index"></view>
				</view>
			</view>
			<checkDem @checkDem="check_dem" v-if="show_page==2"></checkDem>
		</view>
		<view class='item_cont' style="padding-top:196rpx;" v-if="show_page==1">
			<view class='job_cont' v-if="cont_index==0">
				<jobItem :jobs="jobs"></jobItem>
			</view>
			<view class='mech_cont' v-if="cont_index==1">
				<mechItem :mechs="mechs" :status="1"></mechItem>
			</view>
		</view>
		<view class='item_cont' style="padding-top:154rpx;" v-if="show_page==2">
			<view class='teach_cont'>
				<teacherItem :teachers="teachers"></teacherItem>
			</view>
		</view>
		<view class='no_cont' v-if="show_nav">
			<view class="no_box">
			    <view class='title'>积极的人生，才有更好的收获!</view>
			</view>
			<view class='nav_btn' @click="nav_home">去首页</view>
		</view>
		<screen
			:showMask="show_mask"
			:screens="screens"
			:screenIndex="screenIndex"
			:topHeight="top_height"
			@closeMask="close_mask"
			@checkScreen="check_screens"
			@deleteScreen="delete_screen"
			@confirmScreen="confirm_screen"
			v-if="show_page==2"
		>
		</screen>
	</view>
</template>

<script>
	import JobItem from "../../components/jobItem.vue"
	import MechItem from "../../components/mechItem.vue"
	import TeacherItem from "../../components/teacherItem.vue"
	import Screen from "../../components/screen.vue"
	import CheckDem from "../../components/checkDem.vue"
	import home from "../../api/home.js"
	import util from "../../utils/util.js"
	export default{
		data(){
			return {
				show_page: '',
				city: '',
				focus: false,
				word: '',
				checks: ["职位","单位"],
				cont_index: 0,
				jobs: [],
				mechs: [],
				teachers: [],
				top_height: '',
				show_mask: false,
				check_index: '',
				screenIndex: '',
				screens: '',
				screening: false,
				isRange: 1,
				job_name: '',
				positionId: -1,
				positionChildId: -1,
				educats: [],
				educat_id: '',
				educat_index: -2,
				expers: [],
				yearMax: '',
				yearMin: '',
				exper_index: -2,
				wages: [],
				salaryRangeMax: '',
				salaryRangeMin: '',
				wage_index: -2,
				show_nav: false,
				currentPage: 1,
				pageSize: 10,
				hasMoreData: true,
				latitude: 22.820794,
				longitude: 113.284565,
				user_address: ''
			}
		},
		onLoad(options){
			var that=this;
			that.show_page=options.type;
			that.city=options.city
			if(options.type==2){
				that.get_teacher_list();
				that.get_education_list();
				that.get_exper_list();
				that.get_salary_list();
			}else{
				that.get_job_list();
			}
			uni.getStorage({
				key: 'user_address',
				success(res) {
					that.user_address=res.data
					that.latitude=res.data.lat;
					that.longitude=res.data.lng;
				}
			})
		},
		onShow() {
			var that=this;
			if(that.screening){
				that.screening=false;
				that.show_nav=false;
				that.currentPage=1;
				that.teachers=[];
				that.get_teacher_list();
			}
		},
		onReady() {
			var that=this;
			const query = uni.createSelectorQuery().in(this);
			query.select('#top').boundingClientRect(data => {
				// console.log(data)
				// #ifdef H5
				that.top_height=data.height+44;
				// #endif
				// #ifdef MP-WEIXIN || APP-PLUS
				that.top_height=data.height;
				// #endif
			}).exec();
		},
		// 触底
		onReachBottom() {
			var that=this;
			if(that.hasMoreData){
				if(this.show_page==1){
					if(this.cont_index==0){
						that.get_job_list();
					}else{
						that.get_mech_list();
					}
				}else{
					that.get_teacher_list();
				}
			}else{
				util.page_show_toast('没有更多了',1500)
			}
		},
		methods:{
			// 输入框
			input_search(e){
				this.word=e.detail.value;
				this.focus=true;
			},
			// 搜索
			confirm_search(e){
				var word=e.detail.value?e.detail.value:this.word;
				// if(!word||util.testWord(word)){
				//     util.page_show_toast('请输入关键字搜索',1500);
				// }else{
				    word=util.removeAllSpace(word);
					this.word=word;
					this.job_name=word;
					this.show_nav=false;
				    this.currentPage=1;
					this.focus=false;
					if(this.show_page==1){
						if(this.cont_index==0){
							this.jobs=[];
							this.get_job_list();
						}else{
							this.mechs=[];
							this.get_mech_list();
						}
					}else{
						this.teachers=[];
						this.get_teacher_list();
					}
				// }
			},
			// 选择(老师)
			check_cont(index){
				// if(!this.word||util.testWord(this.word)){
				//     util.page_show_toast('请输入关键字搜索',1500);
				// 	return false;
				// }
				this.cont_index=index;
				this.show_nav=false;
				this.currentPage=1;
				if(index==0){
					this.jobs=[];
					this.get_job_list();
				}else{
					this.mechs=[];
					this.get_mech_list();
				}
			},
			// 选择(职位、学历、经验、薪资)
			check_dem(index){
				this.check_index=index;
				if(this.show_mask){
					return false;
				}
				if(index==1){
					uni.navigateTo({
						url: '../jobType/jobType?&range='+this.isRange+'&id='+this.positionId+'&child_id='+this.positionChildId+'&status=1'
					})
				}else{
					this.show_mask=true;
					switch(index){
						case 2:
							this.screens=this.educats;
							this.screenIndex=this.educat_index;
							break;
						case 3:
							this.screens=this.expers;
							this.screenIndex=this.exper_index;
							break;
						case 4:
							this.screens=this.wages;
							this.screenIndex=this.wage_index;
							break;
					}
				}
			},
			// 关闭mask
			close_mask(){
				this.show_mask=false;
			},
			// 选择
			check_screens(index){
				this.screenIndex=index;
			},
			// 清除
			delete_screen(){
				var type=this.check_index;
				this.screenIndex=-2;
				switch(type){
					case 2:
						if(this.educat_index>-2){
							this.educat_index=-2;
							this.educat_id='';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
					case 3:
						if(this.exper_index>-2){
							this.exper_index=-2;
							this.yearMax='';
							this.yearMin='';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
					case 4:
						if(this.wage_index>-2){
							this.wage_index=-2;
							this.salaryRangeMax='';
							this.salaryRangeMin='';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
				}
				this.close_mask();
			},
			// 确定
			confirm_screen(index){
				var type=this.check_index;
				switch(type){
					case 2:
						if(this.educat_index!=index){
							this.educat_index=index;
							this.educat_id=index=-1?this.educats[index].id:'';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
					case 3:
						if(this.exper_index!=index){
							this.exper_index=index;
							this.yearMax=index!=-1?this.expers[index].rangeMax:'';
							this.yearMin=index!=-1?this.expers[index].rangeMin:'';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
					case 4:
						if(this.wage_index!=index){
							this.wage_index=index;
							this.salaryRangeMax=index!=-1?this.wages[index].salaryRangeMax:'';
							this.salaryRangeMin=index!=-1?this.wages[index].salaryRangeMin:'';
							this.show_nav=false;
							this.currentPage=1;
							this.teachers=[];
							this.get_teacher_list();
						}
						break;
				}
				this.close_mask();
			},
			// 回首页
			nav_home(){
				uni.navigateBack({
					delta: 1
				})
			},
			// 职位列表
			get_job_list(){
				uni.showLoading();
				var params={
					"currentPage": this.currentPage,
					"pageSize": this.pageSize,
					"keyword": this.word,
					"city": this.user_address.city_code||this.city,
					"lat": this.latitude,
					"lng": this.longitude
				};
				home.search_jobs(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
						item.salary = item.salaryMin==0?"面议":`${item.salaryMin/1000}-${item.salaryMax/1000}k/月`
					})
					//
					var jobs=util.extend(this.jobs, list)
					if (list.length < this.pageSize) {
						this.jobs=jobs;
						this.hasMoreData=false;
					} else {
						this.jobs=jobs;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&jobs.length==0){
						this.show_nav=true;
					}
				})
			},
			// 机构列表
			get_mech_list(){
				uni.showLoading();
				var params={
					"currentPage": this.currentPage,
					"pageSize": this.pageSize,
					"status": 2,
					"isCertified": 1,
					"type": 1,
					"keyword": this.word,
					"lat": this.latitude,
					"lng": this.longitude
				}
				home.get_mech_list(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
					})
					//
					var mechs=util.extend(this.mechs, list)
					if (list.length < this.pageSize) {
						this.mechs=mechs;
						this.hasMoreData=false;
					} else {
						this.mechs=mechs;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&mechs.length==0){
						this.show_nav=true;
					}
				})
			},
			// 老师列表
			get_teacher_list(){
				uni.showLoading();
				var params={
					"currentPage": this.currentPage,
					"pageSize": this.pageSize,
					"positionId": this.positionId>=0?this.positionId:'',
					"positionName": this.job_name,
					"positionChildId": this.positionChildId>0?this.positionChildId:'',
					"intentionCityName": this.user_address?this.user_address.city.replace("市",""):'',
					"educationId": this.educat_id,
					"yearMax": this.yearMax?this.yearMax:'',
					"yearMin": this.yearMin?this.yearMin:'',
					"salaryRangeMax": this.salaryRangeMax,
					"salaryRangeMin": this.salaryRangeMin,
					"businessId": uni.getStorageSync("userIdentity")?uni.getStorageSync("userIdentity").businessId:''
				}
				home.get_teacher_list(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.age=new Date().getFullYear()-new Date(item.birthday).getFullYear();
						item.workYear=new Date().getFullYear()-new Date(item.joinWorkTime).getFullYear();
						item.salary = item.salaryMin==0?"面议":`${item.salaryMin/1000}-${item.salaryMax/1000}k/月`
						item.works.forEach((itm)=>{
							itm.start=util.formatTime1(new Date(itm.entryTime),1);
							itm.end=util.formatTime1(new Date(itm.quitTime),1)
						})
						if(item.works.length>0){
							item.maxIndex=util.getMaxIndex(item.works)
						}
					})
					//
					var teachers=util.extend(this.teachers, list)
					if (list.length < this.pageSize) {
						this.teachers=teachers;
						this.hasMoreData=false;
					} else {
						this.teachers=teachers;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&teachers.length==0){
						this.show_nav=true;
					}
				})
			},
			// 学历列表
			get_education_list(){
				home.get_education_list().then(res=>{
					this.educats=res.data.data
				})
			},
			// 经验列表
			get_exper_list(){
				home.get_exper_list().then(res=>{
					this.expers=res.data.data;
				})
			},
			// 薪资列表
			get_salary_list(){
				home.get_salary_list().then(res=>{
					this.wages=res.data.data;
				})
			},
		},
		components: {
			jobItem: JobItem,
			mechItem: MechItem,
			teacherItem: TeacherItem,
			screen: Screen,
			checkDem: CheckDem
		},
	}
</script>

<style lang="scss">
	page{
		background: #F6F7F9;
		.page_cont{
			width: 100%;
			.top_wrap{
				position: fixed;
				/* #ifdef MP-WEIXIN */
				top: 0;
				/* #endif */
				/* #ifdef H5 */
				top: 44px;
				/* #endif */
				left: 0;
				width: 100%;
				box-sizing: border-box;
				background: #fff;
				padding-top: 4rpx;
				z-index: 999;
				.search_wrap{
					margin: 0 32rpx 22rpx 32rpx;
					display: flex;
					flex-direction: row;
					background: #F6F7F9;
					border-radius: 12rpx;
					.search_box{
						flex: 1;
						padding: 12rpx;
						display: flex;
						flex-direction: row;
						align-items: center;
						.icon{
							width: 32rpx;
							height: 32rpx;
							margin-right: 8rpx;
						}
						.input{
							flex: 1;
							height: 40rpx;
							font-size: 28rpx;
							font-weight: 500;
							line-height: 40rpx;
							color: rgba(146,155,167,0.81);
						}
					}
					.search_btn{
						width: 120rpx;
						height: 64rpx;
						text-align: center;
						line-height: 64rpx;
						background: #5B90FF;
						border-radius: 12rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: rgba(255,255,255,0.81)
					}
				}
				.check_warp{
					padding: 32rpx 188rpx 8rpx 188rpx;
					display: flex;
					flex-direction: row;
					.item{
						width: 72rpx;
						font-weight: 400;
						color: #929BA7;
						&:first-child{
							margin-right: 160rpx;
						}
						.text{
							width: 72rpx;
							height: 50rpx;
							font-size: 36rpx;
							line-height: 50rpx;
							margin-bottom: 4rpx;
						}
						.line{
							width: 52rpx;
							margin: auto;
							height: 8rpx;
							background: #5B90FF;
							border-radius: 2rpx;
						}
					}
					.active{
						color: #3F4A58;
						font-weight: 600;
					}
				}
			}
			.item_cont{
				width: 100%;
			}
			.no_cont{
				width: 520rpx;
				margin: 110rpx auto 0 auto;
				.no_box{
					width: 100%;
					height: 520rpx;
					box-sizing: border-box;
					padding-top: 360rpx;
					background: url("https://ossdev0.jiabala.com/4B57SRMriNNbG") no-repeat 0 0/100% 100%;
					.title{
						text-align: center;
						height: 50rpx;
						font-size: 32rpx;
						font-weight:400;
						line-height: 50rpx;
						color: #3F4A58;
					}
					.text{
						margin-top: 8rpx;
						text-align: center;
						height: 40rpx;
						font-size: 28rpx;
						font-weight:400;
						line-height: 40rpx;
						color: #798594;
					}
				}
				.nav_btn{
					width: 272rpx;
					margin: auto;
					height: 88rpx;
					background: #5B90FF;
					text-align: center;
					font-size: 30rpx;
					font-weight: 400;
					line-height: 88rpx;
					color:#fff;
					letter-spacing:2rpx;
					border-radius: 16rpx;
				}
			}
		}
	}
</style>
